package com.boarsoft.boar.soagov.com.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.entity.ComInfo;
import com.boarsoft.boar.entity.ObjFaces;
import com.boarsoft.boar.entity.SoaNexus;
import com.boarsoft.boar.soagov.ObjFacesBiz;
import com.boarsoft.boar.soagov.com.ComBiz;
import com.boarsoft.boar.soagov.nexus.NexusBiz;
import com.boarsoft.common.Authorized;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.common.util.JsonUtil;
import com.boarsoft.common.util.RandomUtil;

@RestController
@RequestMapping("/com")
public class ComAction {
	@Autowired
	private ComBiz comBiz;
	@Autowired
	private ObjFacesBiz objFacesBiz;
	@Autowired
	private NexusBiz nexusBiz;
	//
	private String id;
	private String key;// 查询用
	private String code;
	private String name;
	private String type;
	private String memo;
	private String faceId;
	private String impl;
	private String ver;
	private String json;

	/**
	 * 按应用ID分页查询所有的组件/服务<br/>
	 * 组件/服务只能位于某个应用下
	 */
	@RequestMapping("/list")
	public ReplyInfo<String> list(String orderBy, int pageNo, int pageSize) {
		PagedResult<ComInfo> pr = comBiz.list(key, orderBy, pageNo, pageSize);
		return new ReplyInfo<String>(true, JsonUtil.from(pr, "lib,face"));
	}

	@RequestMapping("/save")
	public ReplyInfo<String> save() {
		ComInfo c = null;
		boolean isNew = Util.strIsEmpty(id);
		if (isNew) {
			c = new ComInfo();
			c.setId(RandomUtil.randomUUID());
		} else {
			c = comBiz.get(id);
		}
		c.setCode(code);
		c.setName(name);
		c.setType(type);
		c.setFaceId(faceId);
		c.setImpl(impl);
		c.setVer(ver);
		if (Util.strIsNotEmpty(json)) {
			c.setJson(json);
		}
		c.setMemo(memo);
		if (isNew) {
			comBiz.save(c);
		} else {
			comBiz.update(c);
		}
		return new ReplyInfo<String>(true, JsonUtil.from(c));
	}

	@RequestMapping("/delete.do")
	@Authorized(code = "com.delete")
	public ReplyInfo<Object> delete() {
		comBiz.delete(id);
		return ReplyInfo.SUCCESS;
	}

	@RequestMapping("/get.do")
	@Authorized(code = "com.get")
	public String get() {
		ComInfo co = comBiz.get(id);
		StringBuilder sb = new StringBuilder();
		sb.append("{ \"success\": true, \"data\": ");
		sb.append(JsonUtil.from(co, "lib")).append(", \"byMe\": { \"0\": {}");
		List<SoaNexus> byLt = nexusBiz.listByMe(id);
		for (SoaNexus n : byLt) {
			sb.append(", \"").append(n.getObjId1()).append("\": ");
			sb.append(JsonUtil.from(n, "face,obj0,lib"));
		}
		sb.append(" }, \"onMe\": { \"0\": {}");
		List<SoaNexus> onLt = nexusBiz.listOnMe(id);
		for (SoaNexus n : onLt) {
			sb.append(", \"").append(n.getObjId1()).append("\": ");
			sb.append(JsonUtil.from(n, "face,obj1,lib"));
		}
		sb.append(" }, \"faces\": { \"0\": {}");
		PagedResult<ObjFaces> cfPr = objFacesBiz.list(id, "", "", "", "", 1, 9999);
		for (ObjFaces n : cfPr.getList()) {
			sb.append(", \"").append(n.getFaceId()).append("\": ");
			sb.append(JsonUtil.from(n.getFace(), "com"));
		}
		sb.append(" }}");
		return sb.toString();
	}

	public String getCode() {
		return code;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public ComBiz getComBiz() {
		return comBiz;
	}

	public void setComBiz(ComBiz comBiz) {
		this.comBiz = comBiz;
	}

	public String getFaceId() {
		return faceId;
	}

	public void setFaceId(String faceId) {
		this.faceId = faceId;
	}

	public String getVer() {
		return ver;
	}

	public void setVer(String ver) {
		this.ver = ver;
	}

	public String getImpl() {
		return impl;
	}

	public void setImpl(String impl) {
		this.impl = impl;
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public ObjFacesBiz getObjFacesBiz() {
		return objFacesBiz;
	}

	public void setObjFacesBiz(ObjFacesBiz objFacesBiz) {
		this.objFacesBiz = objFacesBiz;
	}

	public NexusBiz getNexusBiz() {
		return nexusBiz;
	}

	public void setNexusBiz(NexusBiz nexusBiz) {
		this.nexusBiz = nexusBiz;
	}
}